<?php
class Data_Model_NguoiDungMapper extends Data_Model_DataMapperAbstract{
	private static $_instance = null;
	private $_paginator = null;
	
	public function searchById($id)
	{
		$db = $this->getDb();
		$select = $db->select()
					 ->from('tbl_nguoidung')->where('MaND = ?', $id);
		$nguoidung = $db->fetchRow($select);
		
		return $this->_populate($nguoidung);			 
	}
	
	public function nguoidungList()
	{
		$db = $this->getDb();
		$selectNguoidung = $db->select()
							  ->from('tbl_nguoidung');
		$nguoidungs = $db->fetchAll($selectNguoidung);
	
		$nguoidungsObjArray = array();
		foreach($nguoidungs as $nguoidung)
		{
		$nguoidungsObjArray[] = $this->_populate($nguoidung);
		}
		return $nguoidungsObjArray;
	}
	protected  function _populate($data)
	{
		$obj = new Data_Model_NguoiDung();
		$obj->setId($data['MaND']);
		$obj->setTennd($data['TenND']);
		$obj->setEmail($data['Email']);
		$obj->setTendangnhap($data['TenDangNhap']);
		$obj->setMatkhau($data['MatKhau']);
		$obj->setKhoa($data['Khoa']);
		$obj->setQuyenhan($data['QuyenHan']);

		return $obj;
	}	
	
	protected function _insert(Data_Model_DomainObjectAbstract $obj)
	{
		$db = $this->getDb();
		$data = array(
				'TenND' 		=> $obj->getTennd(),
				'Email' 		=> $obj->getEmail(),
				'TenDangNhap' 	=> $obj->getTendangnhap(),
				'MatKhau' 		=> $obj->getMatkhau(),
				'Khoa' 			=> $obj->getKhoa(),
				'QuyenHan' 		=> $obj->getQuyenhan()
				);
		$db->insert('tbl_nguoidung',$data);
	}
	
	protected function _update(Data_Model_DomainObjectAbstract $obj)
	{
		$db = $this->getDb();
		$data = array(
				'TenND' 		=> $obj->getTennd(),
				'Email' 		=> $obj->getEmail(),
				'TenDangNhap' 	=> $obj->getTendangnhap(),
				'MatKhau' 		=> $obj->getMatkhau(),
				'Khoa' 			=> $obj->getKhoa(),
				'QuyenHan' 		=> $obj->getQuyenhan()
				);
		$where = $db->quoteInto('MaND = ?', $obj->getId());
		$db->update('tbl_nguoidung',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaND = ?',$id);
		$db->delete('tbl_nguoidung',$where);
	}
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
}